package com.xoms.admin.controller.springmvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
import com.xoms.admin.controller.res.*;
import com.xoms.admin.controller.res.bean.*;
import com.xoms.admin.controller.springmvc.req.*;

/**
 * 角色资源相关接口
 **/
@Controller
@RequestMapping(value = "/role", produces = "application/json")
public class SRoleController {
	@Autowired
	com.xoms.admin.controller.RoleController roleController;

	/**
	 * 增加/修改角色信息
	 **/
	@RequestMapping("/v1/editRole")
	@ResponseBody
	public ObjectResult<RRoleBean> editRole(@Validated EditRoleParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RRoleBean> res = new ObjectResult<RRoleBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.editRole(param, request, response);
	}

	/**
	 * 删除角色信息
	 **/
	@RequestMapping("/v1/delRole")
	@ResponseBody
	public BaseResult delRole(@Validated DelRoleParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.delRole(param, request, response);
	}

	/**
	 * 显示所有角色信息
	 **/
	@RequestMapping("/v1/getRoles")
	@ResponseBody
	public ListResult<RRoleBean> getRoles(@Validated GetRolesParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RRoleBean> res = new ListResult<RRoleBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.getRoles(param, request, response);
	}

	/**
	 * 启用角色
	 **/
	@RequestMapping("/v1/enableRole")
	@ResponseBody
	public BaseResult enableRole(@Validated EnableRoleParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.enableRole(param, request, response);
	}

	/**
	 * 禁用角色
	 **/
	@RequestMapping("/v1/disableRole")
	@ResponseBody
	public BaseResult disableRole(@Validated DisableRoleParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.disableRole(param, request, response);
	}

	/**
	 * 编辑角色排序
	 **/
	@RequestMapping("/v1/editSorts2Role")
	@ResponseBody
	public BaseResult editSorts2Role(@Validated EditSorts2RoleParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.editSorts2Role(param, request, response);
	}

	/**
	 * 增加/修改资源信息
	 **/
	@RequestMapping("/v1/editResource")
	@ResponseBody
	public ObjectResult<RResourceBean> editResource(@Validated EditResourceParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RResourceBean> res = new ObjectResult<RResourceBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.editResource(param, request, response);
	}

	/**
	 * 删除资源信息
	 **/
	@RequestMapping("/v1/delResource")
	@ResponseBody
	public BaseResult delResource(@Validated DelResourceParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.delResource(param, request, response);
	}

	/**
	 * 显示所有资源信息
	 **/
	@RequestMapping("/v1/getResources")
	@ResponseBody
	public ListResult<RResourceBean> getResources(@Validated GetResourcesParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RResourceBean> res = new ListResult<RResourceBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.getResources(param, request, response);
	}

	/**
	 * 启用资源
	 **/
	@RequestMapping("/v1/enableResource")
	@ResponseBody
	public BaseResult enableResource(@Validated EnableResourceParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.enableResource(param, request, response);
	}

	/**
	 * 禁用资源
	 **/
	@RequestMapping("/v1/disableResource")
	@ResponseBody
	public BaseResult disableResource(@Validated DisableResourceParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.disableResource(param, request, response);
	}

	/**
	 * 编辑资源排序
	 **/
	@RequestMapping("/v1/editSorts2Resource")
	@ResponseBody
	public BaseResult editSorts2Resource(@Validated EditSorts2ResourceParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.editSorts2Resource(param, request, response);
	}

	/**
	 * 显示角色下所有资源信息
	 **/
	@RequestMapping("/v1/getRoleResources")
	@ResponseBody
	public ListResult<RHaveResourcesBean> getRoleResources(@Validated GetRoleResourcesParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RHaveResourcesBean> res = new ListResult<RHaveResourcesBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.getRoleResources(param, request, response);
	}

	/**
	 * 设置角色下资源信息
	 **/
	@RequestMapping("/v1/addRoleResources")
	@ResponseBody
	public BaseResult addRoleResources(@Validated AddRoleResourcesParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.addRoleResources(param, request, response);
	}

	/**
	 * 账户-删除角色下资源
	 **/
	@RequestMapping("/v1/delRoleResources")
	@ResponseBody
	public BaseResult delRoleResources(@Validated DelRoleResourcesParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return roleController.delRoleResources(param, request, response);
	}
}
